package com.linjiang.design.builder.v2;

//建造者模式
//将一个复杂对象的创建与他的表示分离，使得同样的过程可以创建不同的表示
public class BuilderTest2 {
    public static void main(String[] args) {
        /*Product product = new Product("11", "222", "333", "444", "555","666");*/

        Product.Builder builder = new Product.Builder();
        //链式调用构建对象
        Product product = builder.productName("ddd")
                .companyName("222")
                .part1("333")
                .part2("444")
                .part3("5555")
                .part4("666")
                .build();

        System.out.println(product);

    }
}

class Product {

    private final String productName;
    private final String companyName;
    private final String part1;
    private final String part2;
    private final String part3;
    private final String part4;

    public Product(String productName, String companyName, String part1, String part2, String part3, String part4) {
        this.productName = productName;
        this.companyName = companyName;
        this.part1 = part1;
        this.part2 = part2;
        this.part3 = part3;
        this.part4 = part4;
    }

    static class Builder{
        private  String productName;
        private String companyName;
        private String part1;
        private String part2;
        private String part3;
        private String part4;

        public Product build(){
            return new Product(this.productName, this.companyName, this.part1, this.part2, this.part3, this.part4);
        }

        public Builder productName(String productName){
            this.productName = productName;
            //实现链式调用
            return this;
        }

        public Builder companyName(String companyName){
            this.companyName = companyName;
            //实现链式调用
            return this;
        }

        public Builder part1(String part1){
            this.part1 = part1;
            //实现链式调用
            return this;
        }

        public Builder part2(String part2){
            this.part2 = part2;
            //实现链式调用
            return this;
        }

        public Builder part3(String part3){
            this.part3 = part3;
            //实现链式调用
            return this;
        }

        public Builder part4(String part4){
            this.part4 = part4;
            //实现链式调用
            return this;
        }
    }

    @Override
    public String toString() {
        return "Product{" +
                "productName='" + productName + '\'' +
                ", companyName='" + companyName + '\'' +
                ", part1='" + part1 + '\'' +
                ", part2='" + part2 + '\'' +
                ", part3='" + part3 + '\'' +
                ", part4='" + part4 + '\'' +
                '}';
    }
}
